package com.yljk.yljkproject.modelcallback;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.hyphenate.EMCallBack;
import com.hyphenate.chat.EMClient;
import com.tencent.mm.opensdk.modelmsg.SendMessageToWX;
import com.tencent.mm.opensdk.modelmsg.WXImageObject;
import com.tencent.mm.opensdk.modelmsg.WXMediaMessage;
import com.yilijk.base.utils.ALog;
import com.yljk.live.common.utils.MediaCountyManager;
import com.yljk.mcconfig.constants.MCARouter;
import com.yljk.servicemanager.constants.RouterApi;
import com.yljk.servicemanager.interfaceapi.HomeBackApi;
import com.yljk.yljkproject.MyApplication;
import com.yljk.yljkproject.ui.CenterActivity;
import com.yljk.yljkproject.utils.PushMessageUtil;
import com.yljk.yljkproject.utils.ReactUtils;
import com.yljk.yljkproject.wxapi.Util;


/**
 * create by shicheng
 *
 * 首页模块回调实现
 * home module interface callback
 *
 */

@Route(path = RouterApi.HOMEONCLICKIMPL)
public class HomeBackImpl implements HomeBackApi {



    @Override
    public void OnClick2Page(Context context, String pluginId, String componentName) {
        ReactUtils.INSTANCE.push(context, pluginId, componentName);
    }

    @Override
    public void OnClick2PageParams(Context context, String pluginId, String componentName, Bundle bundle) {
        ReactUtils.INSTANCE.push(context, pluginId, componentName,bundle);
    }

    @Override
    public void OnLoginInfoInvalid(Context context, String pluginId, String componentName) {

    }

    @Override
    public void imAccount(Activity activity, String kefuUsername, String kefuPassword, boolean isTokenFlag) {



    }

    @Override
    public boolean isImLogin() {
        return EMClient.getInstance().isLoggedInBefore();
    }

    @Override
    public void imAccount(Activity activity, String username, String password) {
//        if (EMClient.getInstance().isLoggedInBefore()){
//            return;
//        }
        //IMChat登录
        EMClient.getInstance().login(username, password, new EMCallBack() {
            @Override
            public void onSuccess() {
                ALog.d("环信登录成功", "onSuccess: ");
                EMClient.getInstance().chatManager().loadAllConversations();
                EMClient.getInstance().groupManager().loadAllGroups();
            }

            @Override
            public void onError(int code, String error) {
                ALog.d("环信登录失败："+error, "code: "+code);
            }

            @Override
            public void onProgress(int progress, String status) {

            }
        });
//        KefuImHelper.getKefuImHelper().loginKefu(kefuUsername,kefuPassword);
        //客服登录
//        ChatClient.getInstance().login(kefuUsername, kefuPassword, new Callback() {
//            @Override
//            public void onSuccess() {
//                ALog.d("IM客服登录成功", "onSuccess: ");
//            }
//
//            @Override
//            public void onError(int code, String error) {
//
//            }
//
//            @Override
//            public void onProgress(int progress, String status) {
//
//            }
//        });
    }

    @Override
    public void onEnvType(String envType) {

//        if (envType!=null&& "test".equals(envType)){
//            BJCASDK.getInstance().setServerUrl(EnvType.INTEGRATE);
//        }else{
//            BJCASDK.getInstance().setServerUrl(EnvType.PUBLIC);
//        }
    }

    @Override
    public void push(Context context, String pluginId, String conponentName) {
        ReactUtils.INSTANCE.push(context, pluginId, conponentName);
    }

    @Override
    public void push(Context context, String pluginId, String conponentName, Bundle bundle) {
        ReactUtils.INSTANCE.push(context, pluginId, conponentName,bundle);
    }

    @Override
    public void shareWxImg(Bitmap bmp,String openId) {
        //初始化 WXImageObject 和 WXMediaMessage 对象
        WXImageObject imgObj = new WXImageObject(bmp);
        WXMediaMessage msg = new WXMediaMessage();
        msg.mediaObject = imgObj;
//设置缩略图
        Bitmap thumbBmp = Bitmap.createScaledBitmap(bmp, 150, 150, true);
        bmp.recycle();
        msg.thumbData = Util.bmpToByteArray(thumbBmp, true);
//构造一个Req
        SendMessageToWX.Req req = new SendMessageToWX.Req();
        req.transaction = buildTransaction();
        req.message = msg;
        req.scene = SendMessageToWX.Req.WXSceneSession;
        req.userOpenId = openId;
//调用api接口，发送数据到微信
        MyApplication.iwxapi.sendReq(req);
    }

    @Override
    public void networkCode(Activity activity,int code) {
        if (code == 401){
            Intent intent = new Intent(activity, CenterActivity.class);
            intent.putExtra("isLogout",true);
            activity.startActivity(intent);
            activity.finish();
        }
    }

    @Override
    public void moveLive() {
        ARouter.getInstance().build(MCARouter.LIVE_TAB_LAYOUT).navigation();
    }

    @Override
    public void toJumeLive(int id) {
        ARouter.getInstance().build(MCARouter.CREATED_DETAIL_LIVE).withInt("roomId", id).navigation();
    }

    @Override
    public void toJumeLivePage(int id,int client) {
        ARouter.getInstance().build(MCARouter.LIVE_PLAYER)
                .withInt("roomId", id)
                .withInt("liveClient", client)
                .navigation();
    }



    @Override
    public void toJumeMeetProtocal(int id) {
            ARouter.getInstance().build(MCARouter.MEETING_PROTOCOL).withInt("meetingId", id).navigation();
    }

    @Override
    public void toJumeMeetList() {
        ARouter.getInstance().build(MCARouter.MEETING_LIST).navigation();
    }

    @Override
    public void toJumpMeeting(int id) {
        ARouter.getInstance().build(MCARouter.MEETING_DETAIL).withInt("meetingId", id).navigation();
    }

    @Override
    public void toJumpMeetcreate(int meetingId) {
        ARouter.getInstance().build(MCARouter.TUI_CREATE).withInt("meetingId", meetingId).navigation();
    }

    @Override
    public void toJumeLiveList() {
        ARouter.getInstance().build(MCARouter.HEALTH_LIVE).navigation();
    }

    @Override
    public void toWeClassMore() {
        ARouter.getInstance().build(MCARouter.CLASSROOM_MAIN).navigation();
    }

    @Override
    public void toWeClassItem(int album_id) {
        ARouter.getInstance().build(MCARouter.ALBUM_DETAIL).withInt("album_id",album_id).navigation();
    }

    @Override
    public void toBannerClick(String urlString) {
        MediaCountyManager.getInstance().pageClick(urlString);
    }

    @Override
    public void toScheme(Context context, String urlString) {
        PushMessageUtil.getInstance().dealMessage(context,urlString,false);
    }


    @Override
    public void init(Context context) {

    }

    private String buildTransaction() {
        return "img" + System.currentTimeMillis();
    }
}
